/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.

	This file is part of Maxsi Engine.

	Maxsi Engine is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi engine is distributed in the hope that it will be useful, but WITHOUT
	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
	FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
	for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Engine. If not, see <http://www.gnu.org/licenses/>.

	MaxsiEngine.dll
	A general purpose C++ library for the Maxsi Technology Project.

	MaxsiIODeviceEpoll.h
	Implements non-blocking networking using the Epoll interface found in the
	Linux Kernel.

******************************************************************************/

#if defined(Maxsi_Support_MaxsiIO) && defined(Maxsi_Support_Epoll)

#ifndef MaxsiIODeviceEpoll_H
#define MaxsiIODeviceEpoll_H

BeginMaxsiNamespace

class	MaxsiIODeviceEpoll;
struct	MaxsiIOHandleEpoll;

//=============================================================================
//	MaxsiIOHandleEpoll - Extensions to the MaxsiIOHandle class.
//=============================================================================
STRUCTLINK MaxsiIOHandleEpoll : MaxsiIOHandle
{
	FLAGS32	State;
	Bytes_t TotalRead;
	Bytes_t TotalWritten;
	bool PrintDebug;
	long NowMS;
	long LastMS;
	long BegunMS;
	float Elapsed;
};

//=============================================================================
//	Define where the MaxsiIODeviceEpoll interface is declared.
//=============================================================================
#undef MAXSI_INTERFACE
#define MAXSI_INTERFACE MAXSI_INTERFACE_IMPLEMENTED_IN_MAXSI_ENGINE

//=============================================================================
//	MaxsiIODeviceEpoll - Implements MaxsiIODevice using the Epoll interface
//	found in the Linux Kernel and allows us to do non-blocking networking.
//============================================================================
CLASSLINK MaxsiIODeviceEpoll : public MaxsiIODevice
{
public:
	MaxsiIODeviceEpoll();
	virtual ~MaxsiIODeviceEpoll();

public:
	int	EpollFD;

public:
	virtual const size_t GetConcurrentListens();
	virtual const size_t GetEpollListSize();

public:
	virtual MaxsiError DoRead(MaxsiIOHandleEpoll* Handle);
	virtual MaxsiError DoWrite(MaxsiIOHandleEpoll* Handle);
	virtual MaxsiError AcceptClient(MaxsiIOHandleEpoll* Handle);

//=============================================================================
//	Copy-pasteable interface MaxsiIODevice from starts here.
//=============================================================================
public:
	virtual FLAGSL32 GetCapabilities() MAXSI_INTERFACE;

public:
	virtual MaxsiError MainLoop() MAXSI_INTERFACE;

public:
	virtual MaxsiError HandleOpenDevice(MaxsiIOEntity* Remote, MaxsiIOSocket* Socket, bool Writable) MAXSI_INTERFACE;
	virtual MaxsiError HandleListen(MaxsiIOEntity* Remote, MaxsiIOSocket* Socket, short Port) MAXSI_INTERFACE;
	virtual MaxsiError HandleConnect(MaxsiIOEntity* Remote, MaxsiIOSocket* Socket, MESTR* HostName, short Port) MAXSI_INTERFACE;
	virtual MaxsiError HandleOpenFile(MaxsiIOEntity* Remote, MaxsiIOSocket* Socket, MESTR* Path, FLAGS32 Mode) MAXSI_INTERFACE; 
	virtual MaxsiError HandleAccept(MaxsiIOEntity* Remote, MaxsiIOSocket* Socket, MaxsiIOSocket ListenSocket) MAXSI_INTERFACE;

public:
	virtual MaxsiError HandleDisconnect(MaxsiIOSocket* Socket) MAXSI_INTERFACE;

public:
	virtual MaxsiError SetUpAsyncIOHandleCallbacks(MaxsiIOHandle* Connection) MAXSI_INTERFACE;
	virtual MaxsiError AttemptRead(MaxsiIOHandle* Connection, BYTE* Buffer, size_t* BufferSize, size_t* Transferred) MAXSI_INTERFACE;
	virtual MaxsiError AttemptWrite(MaxsiIOHandle* Connection, BYTE* Buffer, size_t* BufferSize, size_t* Transferred) MAXSI_INTERFACE;

};
	
EndMaxsiNamespace

#endif

#endif
